import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../bean/LanguageBean.dart';
import '../livedata/LanguageProvider.dart';
import 'package:provider/provider.dart';

import '../config/AppConst.dart';
import '../generated/l10n.dart';
import '../main.dart';
import '../utils/AudioUtils.dart';
import '../utils/ClickUtils.dart';
import '../utils/CommUtils.dart';
import '../utils/SpUtils.dart';
import '../wight/Seting.dart';

//语言设置
class LanguagePage extends StatefulWidget {
  final CallBack? callback;
  LanguagePage({Key? key, this.callback}) : super(key: key);

  @override
  State<LanguagePage> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<LanguagePage> {
  String? language;

  bool _isVoice = true;
  @override
  void initState() {
    language = SpUtils.getInstance()?.getString(AppConst.language);
    bool? isvoice = SpUtils.getInstance()?.getBool2(AppConst.voice);
    if (isvoice != null) {
      _isVoice = isvoice;
    }

    langeList.clear();
    LanguageBean languageBean1 = LanguageBean();
    languageBean1.lg = "en";
    languageBean1.text = "English";
    langeList.add(languageBean1);
    LanguageBean languageBean2 = LanguageBean();
    languageBean2.lg = "zh";
    languageBean2.text = "简体中文";
    langeList.add(languageBean2);
    LanguageBean languageBean3 = LanguageBean();
    languageBean3.lg = "th";
    languageBean3.text = "ภาษาไทย";
    langeList.add(languageBean3);
    LanguageBean languageBean4 = LanguageBean();
    languageBean4.lg = "vi";
    languageBean4.text = "ViệtName";
    langeList.add(languageBean4);
    LanguageBean languageBean5 = LanguageBean();
    languageBean5.lg = "id";
    languageBean5.text = "IndonesiaName";
    langeList.add(languageBean5);
    LanguageBean languageBean6 = LanguageBean();
    languageBean6.lg = "pt";
    languageBean6.text = "Português";
    langeList.add(languageBean6);
    LanguageBean languageBean7 = LanguageBean();
    languageBean7.lg = "es";
    languageBean7.text = "Español";
    langeList.add(languageBean7);
    LanguageBean languageBean8 = LanguageBean();
    languageBean8.lg = "fr";
    languageBean8.text = "Français";
    langeList.add(languageBean8);
    LanguageBean languageBean9 = LanguageBean();
    languageBean9.lg = "de";
    languageBean9.text = "Deutsch";
    langeList.add(languageBean9);
    LanguageBean languageBean10 = LanguageBean();
    languageBean10.lg = "ru";
    languageBean10.text = "русский язык";
    langeList.add(languageBean10);

    super.initState();
  }

  List<LanguageBean> langeList = [];

  @override
  Widget build(BuildContext context) {
     double wight = CommUtils.getWidth(context);
    double hight = CommUtils.getHeight(context);
    return Container(
       height:hight * 0.6,
      // width: ScreenUtil().setHeight(wight * 0.5),
      decoration: BoxDecoration(
        image: DecorationImage(
            image: AssetImage('assets/images/bg_seting.png'), fit: BoxFit.fill),
      ),
      child: Padding(
        padding: EdgeInsets.only(
              left: wight * 0.05,
              top: hight * 0.01,
               right: wight * 0.05,
        ),
        child: ListView(
          children: [
            Container(
             
              child: Stack(
                children: [
                  Align(
                    alignment: Alignment.center,
                    child: Text(
                      S.of(context).setting_language,
                      style: TextStyle(
                          overflow: TextOverflow.ellipsis,
                          fontWeight: FontWeight.bold,
                          fontSize: ScreenUtil().setSp(20),
                          color: Color.fromRGBO(255, 255, 255, 1)),
                      maxLines: 1,
                    ),
                  ),
                  Align(
                      //关闭
                      alignment: Alignment.centerRight,
                      child: GestureDetector(
                        onTap: () {
                          _playAudio();
                          Navigator.pop(context);
                        },
                        child: Image.asset(
                          'assets/images/close.png',
                          fit: BoxFit.contain,
                           width: wight * 0.07,
                              height: hight * 0.05,
                        ),
                      )),
                ],
              ), //上面语言设置
            ), //
             SizedBox(
                  height: hight*0.02,
                ),

            getSeing(),
          ],
        ),
      ),
    );
  }

  /**
 * 中间的语言设置
 */
  Widget getSeing() {
    double wight = CommUtils.getWidth(context);
      double height = CommUtils.getHeight(context);
    return Container(
       width: double.infinity,
      height: ScreenUtil().setHeight(height* 0.374),
      decoration: BoxDecoration(
        image: DecorationImage(
            image: AssetImage('assets/images/middle.png'), fit: BoxFit.fill),
      ),
      child: Padding(
        padding: EdgeInsets.only(
                 top: height  * 0.04,
            bottom: height  * 0.04,
            left: wight * 0.05,
            right: wight * 0.05),
        child: ListView.separated(
            padding: EdgeInsets.all(0),
            //   shrinkWrap: true, // 加上这个就不会因为高度问题报错
            scrollDirection: Axis.vertical,
            itemBuilder: (context, index) {
              return GestureDetector(
                //点击空白页也有反应
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  if (ClickUtils.checkClick()) {
                    language = langeList[index].lg;
                    Provider.of<LanguageProvider>(context, listen: false).changeMode(language!);
                    Navigator.pop(context);
                    _playAudio();
                    if (widget.callback != null) {
                      //延迟一下，否则不改变语言
                      Future.delayed(Duration(milliseconds: 600), () {
                        widget.callback!(1);
                      });
                    }
                  }
                },
                child: Column(mainAxisSize: MainAxisSize.min, children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      AutoSizeText(
                        langeList[index].text!,
                        style: TextStyle(
                          fontSize: ScreenUtil().setSp(15),
                          fontWeight: FontWeight.bold,
                          color: Color.fromRGBO(255, 255, 255, 1),
                          height: 1.2,
                        ),
                        maxLines: 1,
                        textAlign: TextAlign.center,
                      ),
                      getType(langeList[index].lg!),
                    ],
                  ),
                  SizedBox(
                    height: boxDistance,
                  ),
                  Image.asset(
                    'assets/images/line.png',
                    fit: BoxFit.fill,
                  ),
                  SizedBox(
                    height: boxDistance,
                  ),
                ]),
              );
            },
            physics: BouncingScrollPhysics(),
            separatorBuilder: (BuildContext context, int index) =>
                VerticalDivider(),
            itemCount: langeList.length),
      ),
    );
  }

  Widget getType(String tag) {
    if (language != null && language == tag) {
      return Image.asset('assets/images/gou.png',
          width: ScreenUtil().setWidth(24),
          height: ScreenUtil().setHeight(24),
          fit: BoxFit.contain);
    } else {
      return Container();
    }
  }

  /**
 * 播放音乐
 */
  void _playAudio() {
    if (_isVoice) {
      AudioUtils?.getInstance()?.play("seting_.mp3");
    }
  }
}
